const app = getApp();
const cloud = require("../../utils/api.js");
const { getCloudFunctionData } = require("../../utils/cloudRequest.js");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    hide: app.globalData.hide,
    studyList: [],
    theme: "",
    themeType: app.globalData.themeType,
    loading: true,
  },
  goStudy(e) {
    const { type, gather } = e.currentTarget.dataset;
    wx.navigateTo({
      url: "/pages/study/topic/topic?type=" + type + "&gather=" + gather,
    });
  },

  // 共享数据
  async getTopicList() {
    try {
      this.setData({ loading: true });
      const res = await getCloudFunctionData({ gather: "topic_list" });
      this.setData({
        studyList: res.data,
        loading: false,
      });
    } catch (error) {
      this.setData({ loading: false });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getTopicList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === "function" && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1,
        themeType: app.globalData.themeType,
      });
    }
    this.setData({
      themeType: app.globalData.themeType,
      hide: app.globalData.hide,
    });

    const themeType = wx.getStorageSync("themeType");
    if (themeType == "dark") {
      wx.setNavigationBarColor({
        backgroundColor: "#121212",
        frontColor: "#ffffff",
      });
      wx.setBackgroundColor({
        backgroundColor: "#181818", // 窗口的背景色
        backgroundColorTop: "#181818", // 顶部窗口的背景色
        backgroundColorBottom: "#181818", // 底部窗口的背景色
      });
    } else {
      wx.setNavigationBarColor({
        backgroundColor: "#ffffff",
        frontColor: "#000000",
      });
      wx.setBackgroundColor({
        backgroundColor: "#ffffff", // 窗口的背景色
        backgroundColorTop: "#ffffff", // 顶部窗口的背景色
        backgroundColorBottom: "#ffffff", // 底部窗口的背景色
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  async onPullDownRefresh() {
    await this.getTopicList();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},

  // 分享到朋友圈
  onShareTimeline() {},
});
